﻿using System.IO;
using UnityEditor;
using UnityEngine;

public class SetTextureType
{
    public const string TYPE_2D_PATH = "/Resources/Atlas/OutPut";

    [MenuItem("GameEditor/设置当前贴图格式", false, 20)]
    public static void SetCurTexture()
    {
        SetTexture(GetFileList());
    }

    [MenuItem("GameEditor/设置当前贴图格式--手机优化", false, 20)]
    public static void SetSelTexture_Optimized()
    {
        SetTexture(GetFileList(), true);
    }

    [MenuItem("GameEditor/设置所有贴图格式", false, 20)]
    public static void SetAllTexture()
    {
        string[] allAssetPath = GetFileList(TYPE_2D_PATH);
        SetTexture(allAssetPath);
    }

    [MenuItem("GameEditor/设置所有贴图格式--手机优化", false, 20)]
    public static void SetAllTexture_Optimized()
    {
        string[] allAssetPath = GetFileList(TYPE_2D_PATH);
        SetTexture(allAssetPath, true);
    }
    
    [MenuItem("GameEditor/设置当前贴图格式为RGBA32", false, 20)]
    public static void SetCurTexture_RGBA32()
    {
        SetTexture_RGBA32(GetFileList());
    }

    [MenuItem("GameEditor/设置所有贴图格式为RGBA32", false, 20)]
    public static void SetAllTexture_RGBA32()
    {
        SetTexture_RGBA32(GetFileList());
    }

    static void SetTexture(string[] allAssetPath, bool optimizeForPhone = false)
    {
        for (int i = 0; i < allAssetPath.Length; i++)
        {
            string path = allAssetPath[i];
            EditorUtility.DisplayProgressBar("正在设置", path, (i + 1f) / allAssetPath.Length);
            var texImp = AssetImporter.GetAtPath(path) as TextureImporter;
            texImp.textureType = TextureImporterType.Default;
            texImp.npotScale = TextureImporterNPOTScale.ToNearest;
            texImp.isReadable = false;
            texImp.mipmapEnabled = false;
            texImp.textureFormat = TextureImporterFormat.DXT1;//设置默认格式为dxt1
            texImp.anisoLevel = 0;
            texImp.alphaIsTransparency = false;
            texImp.grayscaleToAlpha = false;                //关闭从图像中的亮度信息生成 alpha 通道
            texImp.wrapMode = TextureWrapMode.Clamp;        //设置循环模式为拉伸
            texImp.SetPlatformTextureSettings(BuildTarget.Android.ToString(), optimizeForPhone 
                ? texImp.maxTextureSize / 2 : texImp.maxTextureSize, TextureImporterFormat.ETC_RGB4);
            texImp.SetPlatformTextureSettings(BuildTarget.iOS.ToString(), optimizeForPhone 
                ? texImp.maxTextureSize / 2 : texImp.maxTextureSize, TextureImporterFormat.PVRTC_RGB4);
            AssetDatabase.ImportAsset(texImp.assetPath);
        }
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
    }

    static void SetTexture_RGBA32(string[] allAssetPath)
    {
        for (int i = 0; i < allAssetPath.Length; i++)
        {
            string path = allAssetPath[i];
            EditorUtility.DisplayProgressBar("正在设置", path, (i + 1f) / allAssetPath.Length);
            var texImp = AssetImporter.GetAtPath(path) as TextureImporter;
            texImp.textureType = TextureImporterType.Default;
            texImp.npotScale = TextureImporterNPOTScale.ToNearest;
            texImp.isReadable = false;
            texImp.mipmapEnabled = false;
            texImp.textureFormat = TextureImporterFormat.RGBA32;//设置默认格式为dxt1
            texImp.anisoLevel = 0;
            texImp.alphaIsTransparency = true;
            texImp.grayscaleToAlpha = false;                //关闭从图像中的亮度信息生成 alpha 通道
            texImp.wrapMode = TextureWrapMode.Clamp;        //设置循环模式为拉伸
            texImp.SetPlatformTextureSettings(BuildTarget.Android.ToString(), 2048, TextureImporterFormat.RGBA32);
            texImp.SetPlatformTextureSettings(BuildTarget.iOS.ToString(), 2048, TextureImporterFormat.RGBA32);
            AssetDatabase.ImportAsset(texImp.assetPath);
        }
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
    }

    static string[] GetFileList(string path)
    {
        string fullPath = Application.dataPath + path;
        string[] allAssetPath = Directory.GetFiles(fullPath, "*.png", SearchOption.AllDirectories);
        for (int i = 0; i < allAssetPath.Length; i++)
        {
            allAssetPath[i] = allAssetPath[i].Replace("\\", "/").Replace(Application.dataPath, "Assets");
        }
        return allAssetPath;
    }

    static string[] GetFileList()
    {
        Object[] objs = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
        string[] allPath = new string[objs.Length];
        for (int i = 0; i < objs.Length; i++)
        {
            allPath[i] = AssetDatabase.GetAssetPath(objs[i]);
        }
        return allPath;
    }
}